home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / lib / mutt / mailto-mutt < prev    next >
Encoding:
Text File  |  2011-12-19  |  2.5 KB  |  119 lines

  1. #!/bin/sh
  2. #
  3. # mailto-mutt -- wrapper to be able to use mutt as mailto handler from firefox
  4. #
  5. # To use, surf to Firefox's about:config page and configure the following
  6. # three values:
  7. #   network.protocol-handler.external.mailto boolean true
  8. #   network.protocol-handler.app.mailto string '/path/to/handler'
  9. #   network.protocol-handler.warn-external.mailto boolean false
  10. #
  11. # Copyright ⌐ martin f. krafft <madduck@madduck.net>
  12. # Released under the terms of the Artistic Licence 2.0
  13. # Revision: $Id: mailto-mutt 498 2007-05-12 12:02:10Z madduck $
  14. set -eu
  15.  
  16. case "${1:-}" in
  17.   -d) debug=1; shift;;
  18.   '') exit 0;;
  19.   *) :;;
  20. esac
  21.  
  22. url_unescape()
  23. {
  24.   perl -e 'use URI::Escape; while(<>) { print uri_unescape($_); }'
  25. }
  26.  
  27. MAILTO="${@%%\?*}"
  28. ARGS="${@#*\?}"
  29. [ "$ARGS" = "$MAILTO" ] && unset ARGS
  30. MAILTO="$(echo "${MAILTO#mailto:}" | url_unescape)"
  31.  
  32. subject=
  33. cc=
  34. bcc=
  35. body=
  36. mutt_commands=
  37.  
  38. IFS_store="$IFS"
  39. IFS='&'
  40.  
  41. for arg in ${ARGS:-}; do
  42.   value="$(echo "${arg#*=}" | url_unescape | sed -e "s,',\',")"
  43.  
  44.   case "${arg%%=*}" in
  45.     subject|Subject|SUBJECT) subject="$value";;
  46.     cc|Cc|CC) cc="$value";;
  47.     bcc|Bcc|BCC) bcc="$value";;
  48.     body|Body|BODY) body="$value";;
  49.     *)
  50.       mutt_commands="${mutt_commands:+${mutt_commands}
  51.         }-e\"my_hdr ${arg%%=*}: $value\""
  52.       ;;
  53.   esac
  54. done
  55.  
  56. IFS="$IFS_store"
  57.  
  58. break_at_commas()
  59. {
  60.   local atom
  61.   for i; do
  62.     atom="${atom:+$atom }$i"
  63.     case "${atom:-}" in
  64.       *,) echo "${atom%,}"; unset atom;;
  65.     esac
  66.   done
  67.   [ -n "${atom:-}" ] && echo "${atom%,}"
  68. }
  69.  
  70. get_addr_args()
  71. {
  72.   local type
  73.   case "$1" in
  74.     -*) type="$1"; shift;;
  75.     *) unset type;;
  76.   esac
  77.  
  78.   break_at_commas "$@" | while read addr; do
  79.     echo -n "${type:-}\"$addr\" "
  80.   done
  81. }
  82.  
  83. if [ -n "$mutt_commands" ]; then
  84.   mutt_args="${mutt_args:+$mutt_args }$mutt_commands"
  85. fi
  86.  
  87. if [ -n "$subject" ]; then
  88.   mutt_args="${mutt_args:+$mutt_args }-s\"$subject\""
  89. fi
  90.  
  91. mutt_args="${mutt_args:+$mutt_args }$(get_addr_args -c $cc)"
  92. mutt_args="${mutt_args:+$mutt_args }$(get_addr_args -b $bcc)"
  93. mutt_args="${mutt_args:+$mutt_args }-- $(get_addr_args $MAILTO)"
  94.  
  95. run()
  96. {
  97.   if [ ${debug:-0} -eq 1 ]; then
  98.     echo "$@"
  99.     [ -t 0 ] && [ -t 1 ] && [ -t 2 ] && $SHELL
  100.   else
  101.     eval "$@"
  102.   fi
  103. }
  104.  
  105. if [ -n "$body" ]; then
  106.   TMPFILE="$(tempfile -p mailto -d /tmp)"
  107.   trap "rm -f $TMPFILE" 0 1 2 3 4 5 6 7 8 10 11 12 13 14 15
  108.   echo "$body" > $TMPFILE
  109.   run x-terminal-emulator -e mutt -i $TMPFILE${mutt_args:+ $mutt_args}
  110.   ret=$?
  111.   rm -f $TMPFILE
  112.   trap - 0 1 2 3 4 5 6 7 8 10 11 12 13 14 15
  113.   exit $ret
  114. else
  115.   run exec x-terminal-emulator -e mutt${mutt_args:+ $mutt_args}
  116. fi
  117.